package com.mipo.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mipo.util.DBUtil;
import com.mipo.vo.Employee;
import com.mipo.vo.Message;
import com.mipo.vo.MessageReceive;

public class MessageReceiveDAOImp implements MessageReceiveDAO {

	public List<MessageReceive> queryMessage(Employee emp) throws Exception {
		List<MessageReceive> list = new ArrayList<MessageReceive>();
		String sql = " select r.state,m.sendId,m.sendName,m.title,m.sendTime,r.rid,m.messageId from employee e,message m,messagereceive r where e.empid=r.receiveId and r.mid=m.messageId and"
				+ " r.receiveId=? order by r.rid desc";
//		if (null != emp.getMessFlag()) {
//			sql = sql + " and r.state=?";
//		}
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, emp.getEmpId());
//		if (null != emp.getMessFlag()) {
//			pst.setInt(2, Integer.valueOf(emp.getMessFlag()));
//		}
		System.out.println("queryMessage:    "+sql);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			MessageReceive mr = new MessageReceive();
			mr.setState(rs.getInt(1));
			mr.setRid(rs.getInt(6));
			Message ms = new Message();
			ms.setSendId(rs.getInt(2));
			ms.setSendName(rs.getString(3));
			ms.setTitle(rs.getString(4));
			ms.setSendTime(rs.getString(5).substring(0,19));
			ms.setMessageId(rs.getInt(7));
			mr.setMss(ms);
			mr.setMCOUNT(mr.getMCOUNT() + 1);
			
			list.add(mr);
		}
		DBUtil.close(conn);
		return list;
	}
	
	public MessageReceive queryMRCOUNT(Employee emp) throws Exception {
		String sql="select count(*) from messagereceive where receiveid=?";
		if(1==emp.getState()){
			sql=sql+" and state=1";
		}else if(1==emp.getState()){
			sql=sql+" and state=0";
		}
		return null;
	}
	
	public boolean insertMessageReceive(MessageReceive mr) throws Exception {
//		 rid | mid | receiveId | seeTime  | state | receiveName |
		String sql = "insert into MessageReceive values(null,?,?,?,?,?)";
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		System.out.println("insertMessageReceive:      "+sql);
		ps.setInt(1, mr.getMid());
		ps.setInt(2, mr.getReceiveId());
		ps.setString(3, mr.getSeeTime());
		ps.setInt(4, mr.getState());
		ps.setString(5, mr.getRiceiveName());
		int r = ps.executeUpdate();
		DBUtil.close(conn);
		return r > 0 ? true : false;
	}
	
	public List<MessageReceive> queryMessageHavingSend(Employee emp)
			throws Exception {
		List<MessageReceive> list = new ArrayList<MessageReceive>();
		String sql = " select r.state,r.receiveId,m.sendName,m.title,m.sendTime,r.rid,r.receiveName,m.messageId from employee e,message m,messagereceive r where e.empid=r.receiveId and r.mid=m.messageId and"
				+ " m.sendId=?  order by r.rid desc";
		if (null != emp.getMessFlag()) {
			sql = sql + " and r.state=?";
		}
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, emp.getEmpId());
		if (null != emp.getMessFlag()) {
			pst.setInt(2, Integer.valueOf(emp.getMessFlag()));
		}
		System.out.println("queryMessage:    "+sql);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			MessageReceive mr = new MessageReceive();
			mr.setState(rs.getInt(1));
			mr.setReceiveId(rs.getInt(2));
			mr.setRid(rs.getInt(6));
			mr.setRiceiveName(rs.getString(7));
			Message ms = new Message();
			ms.setSendName(rs.getString(3));
			ms.setTitle(rs.getString(4));
			ms.setSendTime(rs.getString(5).substring(0,19));
			ms.setMessageId(rs.getInt(8));
			mr.setMss(ms);
			mr.setMCOUNT(mr.getMCOUNT() + 1);
			
			list.add(mr);
		}
		DBUtil.close(conn);
		return list;
	}
	
	public boolean updateMessageReceive(MessageReceive mr) throws Exception {
//		update MessageReceive set state=1 where rid=1;
		String sql = "update MessageReceive set state=1,seeTime=? where rid=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		System.out.println("updateMessageReceive:      "+sql+mr.getRid());
		ps.setString(1, String.valueOf(new Date().toLocaleString()));
//		ps.setInt(2, 1);
		ps.setInt(2, mr.getRid());
		int r = ps.executeUpdate();
		System.out.println(r);
		DBUtil.close(conn);
		return r > 0 ? true : false;
	}
	
	public static void main(String[] args) throws Exception {
		MessageReceive mr=new MessageReceive();
		mr.setRid(2);
		new MessageReceiveDAOImp().updateMessageReceive(mr);
	}
}
